#include "dialogdevice.h"
#include "ui_dialogdevice.h"
#include <QByteArray>
#include "json_list.h"
#include "cJSON/cJSON.h"
#include "cJSON.h"
#include "mem_malloc.h"
#include <stdio.h>

const QString Def_Model = "OBDII-4G";
const char* list_path="./upload/Device.list";
const struct device_node def_node = {
    "up_0",
    "102905420018",
    "OBDII-4G",
    "OBDII-4G.bin",
    "OBDII-4G.cfg"
};

DialogDevice::DialogDevice(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogDevice)
{
    //FILE* fd = NULL;
    struct device_node _node;
    QString Def_Firmware = Def_Model+".bin";
    QString Def_Config   = Def_Model+".cfg";
    QString Def_SN       = "102905420018";

    ui->setupUi(this);
    ui->lineEdit_FW->setEnabled(false);
    ui->lineEdit_CFG->setEnabled(false);
    ui->lineEdit_Info->setEnabled(false);
    ui->checkBox_Lock->setChecked(true);
    ui->lineEdit_Model->setText(Def_Model);
    ui->label_File->setText("Path: "+QString(list_path));
    //QString path = ui->lineEdit_Model->text()+".list";
    memset(&_node, 0, sizeof (_node));
    item_sum = 1;
    item_index = 1;
//    // default value
//    memcpy(_node.Model, Def_Model.toLatin1().data(), Def_Model.length());
//    memcpy(_node.Firmware, Def_Firmware.toLatin1().data(), Def_Firmware.length());
//    memcpy(_node.Config, Def_Config.toLatin1().data(), Def_Config.length());
//    memcpy(_node.SN, Def_SN.toLatin1().data(), Def_SN.length());
//    fd = fopen(path.toLatin1().data(), "r");
//    if(NULL==fd)
//    {
//        create_list(path.toLatin1().data());
//    }
//    else
//    {
//        fclose(fd);
//    }
//    item_index = 1;
//    item_sum = json_list_read(path.toLatin1().data(), item_index, JSON_LIST_UP, &_node);
//    if(item_index>item_sum) item_index=item_sum;
//    fflush_node(&_node);
    read_list_item(++item_index, &def_node);
    //read_list_item(++item_index, &def_node);
#if 1
    char Firmware[64];
    char Config[64];
    const char sn[] = "102906420258";
    const char model[] = "OBDII-4G";
    const char ListPath[] = "./upload/Device.list";
    memset(Firmware, 0, sizeof (Firmware));
    memset(Config, 0, sizeof (Config));
    //if(0==json_list_search("./upload/Device.list", (const char*)(decode->data), JSON_LIST_UP)) upload_flag=1;
    if(0==json_list_search(ListPath, sn, JSON_LIST_UP, Firmware, Config))
    {
        printf("SN:%s FW:%s CFG:%s \n", sn, Firmware, Config); fflush(stdout);
    }
    memset(Firmware, 0, sizeof (Firmware));
    memset(Config, 0, sizeof (Config));
    if(0==json_list_search(ListPath, model, JSON_LIST_MODEL, Firmware, Config))
    {
        printf("Model:%s FW:%s CFG:%s \n", model, Firmware, Config); fflush(stdout);
    }
    printf("Push:%d \n", json_list_search(ListPath, sn, JSON_LIST_PUSH, Firmware, Config)); fflush(stdout);
    if(0==json_list_search(ListPath, sn, JSON_LIST_COMPLE, Firmware, Config))
    {
        printf("Comple FW:%s CFG:%s \n", Firmware, Config); fflush(stdout);
    }
    printf("Comple:%d \n", json_list_search(ListPath, sn, JSON_LIST_COMPLE, Firmware, Config)); fflush(stdout);
    printf("Hit SN:%s List:%d \n", sn, json_list_search(ListPath, sn, JSON_LIST_HIT, Firmware, Config)); fflush(stdout);
#endif
}

DialogDevice::~DialogDevice()
{
    delete ui;
}

void DialogDevice::fflush_node(const device_node * const _node)
{
    ui->lineEdit_Model->setText(_node->Model);
    ui->lineEdit_SN->setText(_node->SN);
    ui->lineEdit_FW->setText(_node->Firmware);
    ui->lineEdit_CFG->setText(_node->Config);
    ui->lineEdit_Info->setText("共"+QString::number(item_sum)+"条, 第"+QString::number(item_index)+"条");
}

void DialogDevice::create_list_empty(const char *path)
{
    FILE* fd = NULL;
    fd = fopen(path, "r");
    if(NULL==fd)
    {
        create_list(path);
    }
    else
    {
        fclose(fd);
    }
}

void DialogDevice::read_list_item(const int _index, const struct device_node* const _node)
{
    int sum=0;
    int index;
    struct device_node node;
//    QString path = ui->lineEdit_Model->text()+".list";
//    QByteArray _path = path.toLatin1();
    const char* _path = list_path;

    FILE* fd = fopen(_path, "r");
    if(NULL==fd)
    {
        create_list(_path);
    }
    else
    {
        fclose(fd);
    }
    index = _index;
    //printf("read_list_item0 [%d | %d | %d] : %d | %d\n", _index, index, item_index, sum, item_sum); fflush(stdout);
    if(index>item_sum) index = item_sum;
    if(index<=0) index = 1;
    memset(&node, 0, sizeof (node));

    //printf("read_list_item1 [%d | %d | %d] : %d | %d\n", _index, index, item_index, sum, item_sum); fflush(stdout);

    sum = json_list_read(_path, index, JSON_LIST_UP, &node);
    if(sum>0)
    {
        if(index>sum) index=sum;
        item_sum = sum;
        item_index = index;
        fflush_node(&node);
    }
    else
    {
        item_sum = 1;
        item_index = 1;
        fflush_node(_node);
    }
    //printf("read_list_item2 [%d | %d | %d] : %d | %d\n", _index, index, item_index, sum, item_sum); fflush(stdout);
}

void DialogDevice::on_pushButton_Add_clicked()
{
#if 0
    create_list("Device.list");
    json_list_add("Device.list", "102905420158", JSON_LIST_DEV);
    json_list_add("Device.list", "102905420158", JSON_LIST_UP);
    json_list_del("Device.list", "102905420148", JSON_LIST_DEV);
    //json_list_del("Device.list", "102905420148", JSON_LIST_UP);
    qDebug("json_list_search:%d \n", json_list_search("Device.list", "102905420148", JSON_LIST_UP));
    qDebug("json_list_search:%d \n", json_list_search("Device.list", "102905420128", JSON_LIST_UP));
#endif

    struct device_node _node;
    //FILE* fd = NULL;
    char firmware[64];
    char config[64];

#if 1
    QByteArray sn = ui->lineEdit_SN->text().toLatin1();
    QByteArray Model = ui->lineEdit_Model->text().toLatin1();
    QByteArray Firmware = ui->lineEdit_FW->text().toLatin1();
    QByteArray Config = ui->lineEdit_CFG->text().toLatin1();
    memset(&_node, 0, sizeof (_node));
    memcpy(_node.SN, sn.data(), sn.length());
    memcpy(_node.Model, Model.data(), Model.length());
    memcpy(_node.Firmware, Firmware.data(), Firmware.length());
    memcpy(_node.Config, Config.data(), Config.length());
//    QString path = ui->lineEdit_Model->text()+".list";
//    QByteArray _path = path.toLatin1();
    const char* _path = list_path;
    printf("_node SN: %s %s \n", _node.SN, sn.data());  fflush(stdout);
    printf("_node Model: %s %s \n", _node.Model, Model.data());  fflush(stdout);
    printf("_node Config: %s %s \n", _node.Config, Firmware.data());  fflush(stdout);
    printf("_node Firmware: %s %s \n", _node.Firmware, Config.data());  fflush(stdout);
#else
    const char* sn=NULL;
    const char* Model=NULL;
    const char* Firmware=NULL;
    const char* Config=NULL;
    sn = ui->lineEdit_SN->text().toLatin1().data();
    Model = ui->lineEdit_Model->text().toLatin1().data();
    Firmware = ui->lineEdit_FW->text().toLatin1().data();
    Config = ui->lineEdit_CFG->text().toLatin1().data();
    memset(&_node, 0, sizeof (_node));
    memcpy(_node.SN, sn, strlen(sn));
    memcpy(_node.Model, Model, strlen(Model));
    memcpy(_node.Firmware, Firmware, strlen(Firmware));
    memcpy(_node.Config, Config, strlen(Config));
    QString path = ui->lineEdit_Model->text()+".list";
    printf("_node SN: %s %s \n", _node.SN, sn);  fflush(stdout);
    printf("_node Model: %s %s \n", _node.Model, Model);  fflush(stdout);
    printf("_node Config: %s %s \n", _node.Config, Firmware);  fflush(stdout);
    printf("_node Firmware: %s %s \n", _node.Firmware, Config);  fflush(stdout);
#endif
#if 1
    create_list_empty(_path);
#else
    create_list(_path.data());
#endif
    memset(firmware, 0, sizeof (firmware));
    memset(config, 0, sizeof (config));
    if(0!=json_list_search(_path, _node.SN, JSON_LIST_UP, firmware, config))
        json_list_add_node(_path, JSON_LIST_UP, &_node);
    if(0!=json_list_search(_path, _node.Model, JSON_LIST_MODEL, firmware, config))
        json_list_add_node(_path, JSON_LIST_MODEL, &_node);
    json_list_add_node(_path, JSON_LIST_DEV, &_node);
    item_sum++;
    read_list_item(++item_index, &def_node);
}

void DialogDevice::on_pushButton_Del_clicked()
{
//    QString path = ui->lineEdit_Model->text()+".list";
//    QByteArray _path = path.toLatin1();
    const char* _path = list_path;
    QByteArray sn = ui->lineEdit_SN->text().toLatin1();
    json_list_del(_path, sn.data(), JSON_LIST_DEV);
    json_list_del(_path, sn.data(), JSON_LIST_UP);
    item_sum--;
    read_list_item(--item_index, &def_node);
}

void DialogDevice::on_pushButton_Next_clicked()
{
#if 0
    const char* const sn="102905421158";
    const char* const Model="OBDII-4G";
    const char* const Firmware="OBDII-4G.bin";
    const char* const Config="OBDII-4G.cfg";
    struct device_node _node;
    memcpy(_node.SN, sn, strlen(sn));
    memcpy(_node.Model, Model, strlen(Model));
    memcpy(_node.Firmware, Firmware, strlen(Firmware));
    memcpy(_node.Config, Config, strlen(Config));
    create_list("Device.list");
    json_list_add("Device.list", "102905420158", JSON_LIST_DEV);
    json_list_add("Device.list", "102905421158", JSON_LIST_DEV);
    json_list_add("Device.list", "102905420158", JSON_LIST_UP);
    json_list_add("Device.list", "102905422158", JSON_LIST_UP);
    json_list_add_node("Device.list", JSON_LIST_UP, &_node);
    //json_list_del("Device.list", "10290540148", JSON_LIST_DEV);
    //json_list_del("Device.list", "102905420148", JSON_LIST_UP);
    qDebug("json_list_search:%d \n", json_list_search("Device.list", "102905420148", JSON_LIST_UP));
    qDebug("json_list_search:%d \n", json_list_search("Device.list", "102905420128", JSON_LIST_UP));
#endif
#if 0
    int sum;
    int index;
    struct device_node _node;
    QString path = ui->lineEdit_Model->text()+".list";

    index = item_index+1;
    if(index>item_sum) index = item_sum;
    memset(&_node, 0, sizeof (_node));

    sum = json_list_read(path.toLatin1().data(), index, JSON_LIST_UP, &_node);
    if(sum>0)
    {
        if(index>sum) index=sum;
        item_sum = sum;
        item_index = index;
        fflush_node(&_node);
    }
#endif
    read_list_item(++item_index, &def_node);
}



void DialogDevice::on_pushButton_Prov_clicked()
{
#if 0
    int sum;
    int index;
    struct device_node _node;
    QString path = ui->lineEdit_Model->text()+".list";

    index = item_index-1;
    if(index<=0) index = 1;
    memset(&_node, 0, sizeof (_node));

    sum = json_list_read(path.toLatin1().data(), index, JSON_LIST_UP, &_node);
    if(sum>0)
    {
        if(index>sum) index=sum;
        item_sum = sum;
        item_index = index;
        fflush_node(&_node);
    }
#endif
    read_list_item(--item_index, &def_node);
}

void DialogDevice::on_lineEdit_Model_textChanged(const QString &arg1)
{
    bool lock = ui->checkBox_Lock->isChecked();
    if(true==lock)
    {
        ui->lineEdit_FW->setText(arg1+".bin");
        ui->lineEdit_CFG->setText(arg1+".cfg");
    }
}

void DialogDevice::on_checkBox_Lock_clicked()
{
    bool lock = ui->checkBox_Lock->isChecked();
    ui->lineEdit_FW->setEnabled(!lock);
    ui->lineEdit_CFG->setEnabled(!lock);
}
